package org.belkodevelop.controller;

import org.belkodevelop.controller.helper.AuthUtils;
import org.belkodevelop.controller.interceptor.UserInterceptor;
import org.belkodevelop.journal.domain.JournalItem;
import org.belkodevelop.journal.dto.JournalItemDto;
import org.belkodevelop.journal.service.JournalService;
import org.belkodevelop.user.service.UserService;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/cabinet")
public class CabinetController {

    @Autowired
    private JournalService journalService;

    @Autowired
    private AuthUtils authUtils;

    @Autowired
    private DozerBeanMapper mapper;

    @Autowired
    private UserService userService;

    @RequestMapping({"/", "", "index.html"})
    public ModelAndView cabinet() {
        ModelAndView modelAndView = new ModelAndView("patient/cabinet");
        modelAndView.addObject("journalItems", journalService.getJournalItems(null, null, authUtils.getCurrentSessionUser().getId()));
        return modelAndView;
    }

    @RequestMapping("/mydoctor.html")
    public ModelAndView mydoctor() {
        ModelAndView modelAndView = new ModelAndView("patient/mydoctor");
        modelAndView.addObject(UserInterceptor.USER_OBJECT_NAME, authUtils.getCurrentUser());
        return modelAndView;
    }

    @RequestMapping(value = "/addJournalItem", method = RequestMethod.POST)
    public String addJournalItem(JournalItemDto journalItemDto) {
        JournalItem journalItem = mapper.map(journalItemDto, JournalItem.class);
        journalItem.setUser(authUtils.getCurrentSessionUser());
        journalService.addJournalItem(journalItem);
        return "redirect:/cabinet";
    }

    @RequestMapping(value = "/deleteJournalItem/{id}", method = RequestMethod.GET)
    public String deleteJournalItem(@PathVariable("id") Long id) {
        if (id != null) {
            journalService.deleteJournalItem(id, authUtils.getCurrentSessionUser());
        }
        return "redirect:/cabinet";
    }

    @RequestMapping(value = "/connectToDoctor.do", method = RequestMethod.POST)
    public ModelAndView connectToDoctor(@RequestParam String emailDoctor) {
        if (emailDoctor != null) {
            boolean result = userService.connectPatientToDoctor(authUtils.getCurrentSessionUser().getId(), emailDoctor);
            if (!result) {
                ModelAndView modelAndView = new ModelAndView("patient/mydoctor");
                modelAndView.addObject("error", "cabinet.doctor_not_found");
                return modelAndView;
            }
        }
        return new ModelAndView("redirect:/cabinet/mydoctor.html");
    }

    @RequestMapping(value = "/resetConnectToDoctor.do", method = RequestMethod.POST)
    public String resetDoctor() {
        userService.resetConnectPatientToDoctor(authUtils.getCurrentSessionUser().getId());
        return "redirect:/cabinet/mydoctor.html";
    }

}
